package com.project.server.companies;

import com.project.common.annotation.Log;
import com.project.common.core.controller.BaseController;
import com.project.common.core.domain.AjaxResult;
import com.project.common.core.page.TableDataInfo;
import com.project.common.enums.BusinessType;
import com.project.common.utils.StringUtils;
import com.project.common.utils.poi.ExcelUtil;
import com.project.server.domain.companies.TCompany;
import com.project.server.service.companies.ITCompanyService;
import com.project.server.service.estates.ITPayRuleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 企业信息Controller
 *
 * @author project
 * @date 2021-07-01
 */
@RestController
@RequestMapping("/server/company")
public class TCompanyController extends BaseController {

    @Resource
    private ITCompanyService tCompanyService;

    @Resource
    private ITPayRuleService tPayRuleService;

    /**
     * 查询企业信息列表
     */
    //@PreAuthorize("@ss.hasPermi('server:company:list')")
    @GetMapping("/list")
    public TableDataInfo list(TCompany tCompany) {
        startPage();
        List<TCompany> list = tCompanyService.selectTCompanyList(tCompany);
        return getDataTable(list);
    }


    /**
     * 导出企业信息列表
     */
    @PreAuthorize("@ss.hasPermi('server:company:export')")
    @Log(title = "企业信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TCompany tCompany) {
        List<TCompany> list = tCompanyService.selectTCompanyList(tCompany);
        ExcelUtil<TCompany> util = new ExcelUtil<TCompany>(TCompany.class);
        return util.exportExcel(list, "入园企业列表");
    }

    /**
     * 获取企业信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('server:company:query')")
    @GetMapping(value = {"/","/{compId}"})
    public AjaxResult getInfo(@PathVariable(value = "compId", required = false) Long compId) {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("payRules", tPayRuleService.selectTPayRuleAll());
        if (StringUtils.isNotNull(compId)) {
            ajax.put(AjaxResult.DATA_TAG, tCompanyService.selectTCompanyById(compId));
            ajax.put("ruleId", tPayRuleService.selectTCompanyByCompId(compId));
        }
        return ajax;
    }

    /**
     * 新增企业信息
     */
    @PreAuthorize("@ss.hasPermi('server:company:add')")
    @Log(title = "企业信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody TCompany tCompany) {
        return AjaxResult.success(tCompanyService.insertTCompany(tCompany));
    }

    /**
     * 修改企业信息
     */
    @PreAuthorize("@ss.hasPermi('server:company:edit')")
    @Log(title = "企业信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody TCompany tCompany) {
        return toAjax(tCompanyService.updateTCompany(tCompany));
    }

    /**
     * 删除企业信息
     */
    @PreAuthorize("@ss.hasPermi('server:company:remove')")
    @Log(title = "企业信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{compIds}")
    public AjaxResult remove(@PathVariable Long[] compIds) {
        return toAjax(tCompanyService.deleteTCompanyByIds(compIds));
    }

//    /**
//     * 获取企业选择框列表
//     */
//    @GetMapping("/compSelect")
//    public AjaxResult compSelect() {
//        List<TCompany> comp = tCompanyService.selectTCompanyAll();
//        return AjaxResult.success(comp);
//    }

}
